package hz.mobileproject.pda.model.attendancemanager.activity;

import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.app.MainApp;
import hz.mobileproject.pda.entity.MessageEvent;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.model.attendancemanager.bean.Rp_queryOutReport;
import hz.mobileproject.pda.model.attendancemanager.bean.Rq_deleteOutReport;
import hz.mobileproject.pda.model.attendancemanager.bean.Rq_eventOutReport;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.base.BaseActivity;
import hz.mobileproject.pda.ui.view.ContentSummaryOtherDialog;
import hz.mobileproject.pda.utils.HttpRqUtils;

@ContentView(R.layout.activity_out_data_detail)
public class OutDataDetailActivity extends BaseActivity {
    @ViewInject(R.id.tv_police_name)
    private TextView tv_police_name;
    @ViewInject(R.id.tv_police_code)
    private TextView tv_police_code;
    @ViewInject(R.id.tv_police_dept)
    private TextView tv_police_dept;
    @ViewInject(R.id.tv_admin_post)
    private TextView tv_admin_post;
    @ViewInject(R.id.tv_outgoing_begin_time)
    private TextView tv_outgoing_begin_time;
    @ViewInject(R.id.tv_outgoing_end_time)
    private TextView tv_outgoing_end_time;
    @ViewInject(R.id.tv_outgoing_antifake_time)
    private TextView tv_outgoing_antifake_time;
    @ViewInject(R.id.tv_outgoing_reason)
    private TextView tv_outgoing_reason;
    @ViewInject(R.id.tv_outgoing_day_count)
    private TextView tv_outgoing_day_count;
    @ViewInject(R.id.tv_outgoing_city)
    private TextView tv_outgoing_city;
    @ViewInject(R.id.tv_outgoing_remark)
    private TextView tv_outgoing_remark;
    //分监区审批
    @ViewInject(R.id.ll_fjq_data)
    private LinearLayout ll_fjq_data;
    @ViewInject(R.id.tv_fjq_police_name)
    private TextView tv_fjq_police_name;
    @ViewInject(R.id.tv_fjq_time)
    private TextView tv_fjq_time;
    @ViewInject(R.id.tv_fjq_status_name)
    private TextView tv_fjq_status_name;
    //监区审批
    @ViewInject(R.id.ll_dept_data)
    private LinearLayout ll_dept_data;
    @ViewInject(R.id.tv_dept_police_name)
    private TextView tv_dept_police_name;
    @ViewInject(R.id.tv_dept_time)
    private TextView tv_dept_time;
    @ViewInject(R.id.tv_dept_status_name)
    private TextView tv_dept_status_name;
    //组织科审批
    @ViewInject(R.id.ll_org_data)
    private LinearLayout ll_org_data;
    @ViewInject(R.id.tv_org_police_name)
    private TextView tv_org_police_name;
    @ViewInject(R.id.tv_org_time)
    private TextView tv_org_time;
    @ViewInject(R.id.tv_org_status_name)
    private TextView tv_org_status_name;
    //纪委监察科审批
    @ViewInject(R.id.ll_poloff_data)
    private LinearLayout ll_poloff_data;
    @ViewInject(R.id.tv_poloff_police_name)
    private TextView tv_poloff_police_name;
    @ViewInject(R.id.tv_poloff_time)
    private TextView tv_poloff_time;
    @ViewInject(R.id.tv_poloff_status_name)
    private TextView tv_poloff_status_name;
    //政治处审批
    @ViewInject(R.id.ll_pol_data)
    private LinearLayout ll_pol_data;
    @ViewInject(R.id.tv_pol_police_name)
    private TextView tv_pol_police_name;
    @ViewInject(R.id.tv_pol_time)
    private TextView tv_pol_time;
    @ViewInject(R.id.tv_pol_status_name)
    private TextView tv_pol_status_name;
    //政委审批
    @ViewInject(R.id.ll_branch_data)
    private LinearLayout ll_branch_data;
    @ViewInject(R.id.tv_branch_police_name)
    private TextView tv_branch_police_name;
    @ViewInject(R.id.tv_branch_time)
    private TextView tv_branch_time;
    @ViewInject(R.id.tv_branch_status_name)
    private TextView tv_branch_status_name;

    @ViewInject(R.id.btn_delete)
    private Button btn_delete;
    @ViewInject(R.id.ll_hasApproval)
    private LinearLayout ll_hasApproval;

    private Rp_queryOutReport.OutReportDatas mData;

    @Override
    protected void initData() {
        mData = (Rp_queryOutReport.OutReportDatas) getIntent().getSerializableExtra("key");
        tv_police_name.setText(mData.police_name);
        tv_police_code.setText(mData.police_code);
        tv_police_dept.setText(mData.dept_name);
        tv_admin_post.setText(mData.admin_post);
        tv_outgoing_begin_time.setText(mData.start_date);
        tv_outgoing_end_time.setText(mData.end_date);
        tv_outgoing_antifake_time.setText(mData.antifake_date);
        tv_outgoing_reason.setText(mData.reason_name);
        tv_outgoing_day_count.setText(mData.day + " 天");
        tv_outgoing_city.setText(mData.direction);
        tv_outgoing_remark.setText(mData.remark);
        //是本人  且审批流程没有完成则提供删除按钮
        if (MainApp.getInstance().getLoginUser().getPolice_code().equals(mData.police_code) && mData.flowId != 1 && mData.flowId != 2) {
            btn_delete.setVisibility(View.VISIBLE);
        } else {
            btn_delete.setVisibility(View.GONE);
        }
        //是否有审批权限
        if (mData.hasApproval != 1) {
            ll_hasApproval.setVisibility(View.GONE);
        }else {
            ll_hasApproval.setVisibility(View.VISIBLE);
        }

        //分监区审批
        if (mData.fjqDate.fjq_status == 1 || mData.fjqDate.fjq_status == 2) {
            ll_fjq_data.setVisibility(View.VISIBLE);
            tv_fjq_police_name.setText(mData.fjqDate.fjq_police_name);
            tv_fjq_time.setText(mData.fjqDate.fjq_time);
            if (mData.fjqDate.fjq_status == 1) {
                tv_fjq_status_name.setText(mData.fjqDate.fjq_status_name);
            } else {
                tv_fjq_status_name.setText(mData.fjqDate.fjq_status_name + "(" + mData.fjqDate.fjq_opinion + ")");
                tv_fjq_status_name.setTextColor(ContextCompat.getColor(this, R.color.orangered));
            }

        }
        //监区审批
        if (mData.deptDate.dept_status == 1 || mData.deptDate.dept_status == 2) {
            ll_dept_data.setVisibility(View.VISIBLE);
            tv_dept_police_name.setText(mData.deptDate.dept_police_name);
            tv_dept_time.setText(mData.deptDate.dept_time);
            if (mData.deptDate.dept_status == 1) {
                tv_dept_status_name.setText(mData.deptDate.dept_status_name);
            } else {
                tv_dept_status_name.setText(mData.deptDate.dept_status_name + "(" + mData.deptDate.dept_opinion + ")");
                tv_dept_status_name.setTextColor(ContextCompat.getColor(this, R.color.orangered));
            }

        }
        //组织科审批
        if (mData.orgDate.org_status == 1 || mData.orgDate.org_status == 2) {
            ll_org_data.setVisibility(View.VISIBLE);
            tv_org_police_name.setText(mData.orgDate.org_police_name);
            tv_org_time.setText(mData.orgDate.org_time);
            if (mData.orgDate.org_status == 1) {
                tv_org_status_name.setText(mData.orgDate.org_status_name);
            } else {
                tv_org_status_name.setText(mData.orgDate.org_status_name + "(" + mData.orgDate.org_opinion + ")");
                tv_org_status_name.setTextColor(ContextCompat.getColor(this, R.color.orangered));
            }

        }
        //纪委监察科审批   外出报备没有此流程所以可能为空
        if (mData.polDate != null) {
            if (mData.polDate.pol_status == 1 || mData.polDate.pol_status == 2) {
                ll_pol_data.setVisibility(View.VISIBLE);
                tv_pol_police_name.setText(mData.polDate.pol_police_name);
                tv_pol_time.setText(mData.polDate.pol_time);
                if (mData.polDate.pol_status == 1) {
                    tv_pol_status_name.setText(mData.polDate.pol_status_name);
                } else {
                    tv_pol_status_name.setText(mData.polDate.pol_status_name + "(" + mData.polDate.pol_opinion + ")");
                    tv_pol_status_name.setTextColor(ContextCompat.getColor(this, R.color.orangered));
                }

            }
        }
        //政治处审批
        if (mData.poloffDate.poloff_status == 1 || mData.poloffDate.poloff_status == 2) {
            ll_poloff_data.setVisibility(View.VISIBLE);
            tv_poloff_police_name.setText(mData.poloffDate.poloff_police_name);
            tv_poloff_time.setText(mData.poloffDate.poloff_time);
            if (mData.poloffDate.poloff_status == 1) {
                tv_poloff_status_name.setText(mData.poloffDate.poloff_status_name);
            } else {
                tv_poloff_status_name.setText(mData.poloffDate.poloff_status_name + "(" + mData.poloffDate.poloff_opinion + ")");
                tv_poloff_status_name.setTextColor(ContextCompat.getColor(this, R.color.orangered));
            }

        }
        //政委审批
        if (mData.branchDate.branch_status == 1 || mData.branchDate.branch_status == 2) {
            ll_branch_data.setVisibility(View.VISIBLE);
            tv_branch_police_name.setText(mData.branchDate.branch_police_name);
            tv_branch_time.setText(mData.branchDate.branch_time);
            if (mData.branchDate.branch_status == 1) {
                tv_branch_status_name.setText(mData.branchDate.branch_status_name);
            } else {
                tv_branch_status_name.setText(mData.branchDate.branch_status_name + "(" + mData.branchDate.branch_opinion + ")");
                tv_branch_status_name.setTextColor(ContextCompat.getColor(this, R.color.orangered));
            }

        }
    }

    @Event({R.id.btn_delete, R.id.btn_positive, R.id.btn_negative})
    private void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.btn_delete:
                if (!MainApp.getInstance().getLoginUser().getPolice_code().equals(mData.police_code)) {
                    ToastUtils.showShort("非本人不能删除");
                    return;
                }
                ContentSummaryOtherDialog dialog = new ContentSummaryOtherDialog(this);
                dialog.setContentVisable(false).setTitle("确认删除此条记录？")
                        .setOnClickListener(new ContentSummaryOtherDialog.OnClickListener() {
                            @Override
                            public void onPositiveClick(String s) {
                                delOutgoingRecord(mData);
                            }

                            @Override
                            public void onNagetiveClick() {

                            }
                        }).show();
                break;
            case R.id.btn_positive:
                approval(1, "");
                break;
            case R.id.btn_negative:
                ContentSummaryOtherDialog dialog1 = new ContentSummaryOtherDialog(this);
                dialog1.setTitle("审批意见").setMessage("请填写审批意见").setContentCanNull(false)
                        .setOnClickListener(new ContentSummaryOtherDialog.OnClickListener() {
                            @Override
                            public void onPositiveClick(String s) {
                                approval(2, s);
                            }

                            @Override
                            public void onNagetiveClick() {

                            }
                        }).setContentMaxLength(100).show();
                break;
        }
    }

    /**
     * 调用接口审批
     *
     * @param state   1-通过 2-不通过
     * @param opinion
     */
    private void approval(int state, String opinion) {
        Rq_eventOutReport rq = new Rq_eventOutReport();
        rq.setCusNumber(MainApp.getInstance().getLoginUser().getCus_number() + "");
        rq.setPoliceCode(MainApp.getInstance().getLoginUser().getPolice_code());
        rq.setDept_id(mData.dept_id);
        rq.setRecordId(mData.record_id);
        rq.setFlowId(mData.flowId + "");
        rq.setFlowName(mData.flowName);
        rq.setFlowTypeId("10203");
        rq.setApprovalState(state);
        rq.setApprovalOpinion(opinion);
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_OUTGOING_EVENTOUTREPORT, rq, new CustomNetCallBack() {
            @Override
            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
                LogUtils.e(data);
                ToastUtils.showShort("审批成功");
                EventBus.getDefault().post(new MessageEvent("OutDataDetailActivity.refreshData", ""));
                EventBus.getDefault().post(new MessageEvent("LeaveActivity.refreshData", ""));
                EventBus.getDefault().post(new MessageEvent("GoAbroadActivity.refreshData", ""));
                OutDataDetailActivity.this.finish();
            }

            @Override
            public void onErrorFromSer(int rtnCode, String rtnMsg) {
                LogUtils.e(rtnCode + rtnMsg);
                ToastUtils.showShort("审批失败(" + rtnCode + "," + rtnMsg + ")");
            }

            @Override
            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
                LogUtils.e(ex.toString());
                ToastUtils.showShort("审批失败，请检查网络");
            }
        });
    }


    /**
     * 调用接口删除
     *
     * @param item
     */
    private void delOutgoingRecord(final Rp_queryOutReport.OutReportDatas item) {
        Rq_deleteOutReport rq = new Rq_deleteOutReport();
        rq.setCusNumber(item.cus_number);
        rq.setRecordId(item.record_id);
        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_OUTGOING_DELETEOUTREPORT, rq, new CustomNetCallBack() {
            @Override
            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
                LogUtils.e(data);
                EventBus.getDefault().post(new MessageEvent("OutgoingActivity.deleteData", "", mData));
                EventBus.getDefault().post(new MessageEvent("LeaveActivity.deleteData", "",mData));
                EventBus.getDefault().post(new MessageEvent("GoAbroadActivity.deleteData", "",mData));
                OutDataDetailActivity.this.finish();
            }

            @Override
            public void onErrorFromSer(int rtnCode, String rtnMsg) {
                LogUtils.e(rtnCode + rtnMsg);
                ToastUtils.showShort("删除失败(" + rtnCode + "," + rtnMsg + ")");
            }

            @Override
            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
                LogUtils.e(ex.toString());
                ToastUtils.showShort("删除失败，请检查网络");
            }
        });
    }
}
